#include "web.h"

Web::Web()
{
    // connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(doLogined(QNetworkReply*)));

    auth = QJsonDocument::fromJson("{\"create\":\"2024-09-22T09:57:52\",\"email\":\"3361098@qq.com\",\"id\":1,\"lasttime\":\"2024-10-22T12:37:56.789570\",\"phone\":\"13896370136\",\"status\":1,\"username\":\"tttbbb999\"}").object();
    user = QJsonDocument::fromJson("{\"auth\":1,\"basename\":\"GGG\",\"create\":\"2024-09-22 09:57:52\",\"gender\":1,\"id\":1,\"idcard\":\"511204198105267611\",\"lasttime\":\"2024-10-10 13:55:05\",\"nicename\":\"DDD\"}").object();
    token = "Uearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNzI5NjE1MDc2LCJpYXQiOjE3Mjk1NzE4NzYsImp0aSI6ImJiOGViNmZjMWU0MzQxMWY5YzVhZTZkMDg2NGI0ZTIwIiwidXNlcl9pZCI6MX0.aP2ffhv4ePE7tLXFhQUuU3OZIUN7qHzq1SGMuBvR1Qc";
    // qDebug() << "查询基础数据:" << user;

    if (token.isEmpty()){
       doLogin();
    }
    if(pos.isEmpty()){
        hasMyPos();
    }
    qDebug() << "查询帐户数据:" << auth;
    qDebug() << "查询商户数据:" << pos;
}
Web::~Web()
{
}


void Web::doLogin()
{

    qDebug() << "...login...";
    QUrl url;
    url.setUrl(host +"/user/LoginUser/");

    QNetworkRequest request;
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));

    QByteArray post_param;
    QJsonDocument document;
    QJsonObject post_data;
    post_data.insert("username",username);
    post_data.insert("password",password);
    document.setObject(post_data);    
    post_param = document.toJson();

    QNetworkReply *reply = manager.post(request,post_param);

    connect(reply, &QNetworkReply::finished, this, [this, reply]() {        
        if (reply->error() != QNetworkReply::NoError) {
            qDebug() << "Error:" << reply->errorString();
        } else {            
            QJsonObject resReply = QJsonDocument::fromJson(reply->readAll()).object();
            auth = resReply.value("auth").toObject();
            user = resReply.value("user").toObject();
            token = resReply.value("token").toString();
            qDebug() << "登录成功:" << resReply;
        }
        reply->deleteLater();
    });

}

void Web::hasMyPos()
{    
    if (token.isEmpty()){
        qDebug() << "...not find token...";
        return;
    }

    qDebug() << "...myposlist...";
    QUrl url;
    url.setUrl(host + "/buss/staffall/?page=1&uid=" + auth.value("id").toString());

    QNetworkRequest request;
    request.setUrl(url);
    request.setHeader(QNetworkRequest::ContentTypeHeader, QVariant("application/json"));
    request.setRawHeader("Authorization", token.toLocal8Bit());

    QNetworkReply *reply = manager.get(request);

    connect(reply, &QNetworkReply::finished, this, [this, reply]() {
        if (reply->error() != QNetworkReply::NoError) {
            qDebug() << "Error:" << reply->errorString();
        } else {
            QJsonObject resReply = QJsonDocument::fromJson(reply->readAll()).object();
            QJsonArray allPos = resReply.value("resmsg").toArray();
            // for(int i=0;i<allPos.count();i++){
            //     qDebug() << "查询商户列表:" << allPos[i].toObject();
            // }
            pos = allPos[0].toObject();
        }
        reply->deleteLater();       
    });    
};
